#!/bin/bash
CURRENT_FILE=install.sh
printf "[%s][%010s:%3s] JDK 开始安装...\n" `date +"%T"` $CURRENT_FILE $LINENO
SOURCE_DIR=$PWD
# Step1. Get install packet
for file in ${SOURCE_DIR}/*; do
	if [[ $file =~ jdk.*x64.*\.rpm ]]; then
		JDK_PACK=$file
	fi
done
if [[ ! -f $JDK_PACK ]]; then
	printf "[%s][%010s:%3s] JDK 安装包不存在：\"%s\"\n" `date +"%T"` $CURRENT_FILE $LINENO $JDK_PACK
    exit
fi  
# Step2. Uninstall JDK
for i in $(rpm -qa | grep jdk | grep -v grep)
do
	printf "[%s][%010s:%3s] 卸载已安装 JDK： \"%s\"\n" `date +"%T"` $CURRENT_FILE $LINENO $i
	rpm -e --allmatches $i --nodeps > /dev/null 2>&1
done
# Step3. Install JDK
printf "正在安装JDK..."
sudo rpm -ivh $JDK_PACK
if [ $? -eq 0 ]; then
    printf "[%s][%010s:%3s] JDK 安装完成\n" `date +"%T"` $CURRENT_FILE $LINENO
    # Check version
    java -version
else
    error "JDK安装失败，请尝试手动安装。"
    exit 1
fi